﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Transactions;
using STLGROUP.Layers.DataAccessLayers;
using LinqToExcel;
using STLGROUP.Layers.Common;
using System.Data.Objects.DataClasses;

namespace STLGROUP.Layers.Presentations.FormBase
{
    /// <summary>
    /// frmManager:  cho phep thua ke trong cac FormQuanLy hoac FormReport
    /// </summary>
    /// <author>Viet sang</author>
    /// <date>29/09/2011</date>
    /// 
    public partial class frmManager : frmBindingSource
    {
        #region Variables

        private int? _groupUserID = null;

        private string _moduleNameString = string.Empty;

        private int _detailPanelHeight = 200;
        private string _detailPanelTitle = "Chi Tiết";
        private string _titleText = string.Empty;


        #endregion

        #region Constructors

        public frmManager()
        {
            InitializeComponent();

            tscbxTimKiem.Size = new Size(190, 25);
            //tsbLuu.Enabled = false;
            pnlChiTiet.Enabled = false;
            
            InitForm();              
        }

        #endregion

        #region My Properties
        ///<summary>
        ///Set property for toolStripControls
        ///</summary>
        ///<author>Viet Sang</author>
        ///<date>28/09/2011</date>
        ///

        /// <summary>
        /// Gets or sets a value indicating whether [visible clear button].
        /// </summary>
        /// <value><c>true</c> if [visible clear button]; otherwise, <c>false</c>.</value>
        /// 

        //addnew buttom 
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleAddNewButtom
        {
            get { return tsbThem.Visible; }
            set
            {
                this.tsbThem.Visible = value;
            }
        }

        //Cacel buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleCancelButtom
        {
            get { return tsbCancel.Visible; }
            set
            {
                this.tsbCancel.Visible = value;
            }
        }

        //edit buttom 
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleEditButtom
        {
            get { return tsbSua.Visible; }
            set
            {
                this.tsbSua.Visible = value;
            }
        }

        // delete buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleDeleteButtom
        {
            get { return tsbXoa.Visible; }
            set
            {
                this.tsbXoa.Visible = value;
            }
        }

        // save buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleSaveButtom
        {
            get { return tsbLuu.Visible; }
            set
            {
                this.tsbLuu.Visible = value;
            }
        }

        //separator1
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleToolStripSeparator1
        {
            get { return toolStripSeparator1.Visible; }
            set
            {
                this.toolStripSeparator1.Visible = value;
            }
        }

        //filter buttom 
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleFilterButtom
        {
            get { return tsbLoc.Visible; }
            set
            {
                this.tsbLoc.Visible = value;
            }
        }

        //clear buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleClearButtom
        {
            get { return tsbClear.Visible; }
            set
            {
                this.tsbClear.Visible = value;
            }
        }

        //refresh buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleRefreshButtom
        {
            get { return tsbLamMoi.Visible; }
            set
            {
                this.tsbLamMoi.Visible = value;
            }
        }

        //separator2
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibletoolToolStripSeparator2
        {
            get { return toolStripSeparator2.Visible; }
            set
            {
                this.toolStripSeparator2.Visible = value;
            }
        }

        //report buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleReportButtom
        {
            get { return tsbBaoCao.Visible; }
            set
            {
                this.tsbBaoCao.Visible = value;
            }
        }

        //separator3
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibletoolToolStripSeparator3
        {
            get { return toolStripSeparator3.Visible; }
            set
            {
                this.toolStripSeparator3.Visible = value;
            }
        }


        //search controls
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleSearchControl
        {
            get
            {
                return tscbxTimKiem.Visible = tslblTimKiem.Visible = tsbTimKiem.Visible;
            }
            set
            {
                this.tscbxTimKiem.Visible = this.tsbTimKiem.Visible = this.tslblTimKiem.Visible = value;
            }
        }


        //close buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleCloseButtom
        {
            get { return tsbClose.Visible; }
            set
            {
                this.tsbClose.Visible = value;
            }
        }

        //import Excel buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleImportExcelButtom
        {
            get { return tsbImportExcel.Visible; }
            set
            {
                //this.tsbImportExcel.Visible = value;
                this.tsbImportExcel.Visible = false;
            }
        }

        //Export Excel buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleExportExcelButtom
        {
            get { return tsbExportExcel.Visible; }
            set
            {
                this.tsbExportExcel.Visible = value;
            }
        }

        // toolStripSeparator4 buttom
        [Category("My Properties")]
        [DefaultValue(true)]
        public bool VisibleToolStripSeparator4
        {
            get { return toolStripSeparator4.Visible; }
            set
            {
                this.toolStripSeparator4.Visible = value;
            }
        }


        // height of Detail Panel
        [Category("My Properties")]
        [DefaultValue(true)]
        public int HeightDetailPanel
        {
            get { return _detailPanelHeight; }
            set
            {
                _detailPanelHeight = value;
                this.pnlChiTiet.Height = _detailPanelHeight;
            }
        }

        [Category("My Properties")]
        [DefaultValue(true)]
        public string DetailPanelTitle
        {
            get { return _detailPanelTitle; }
            set
            {
                _detailPanelTitle = value;
                this.pnlChiTiet.TitleText = _detailPanelTitle;
            }
        }

        [Browsable(true)]
        [Category("ModuleName")]
        public string ModuleNameString
        {
            get { return _moduleNameString; }
            set { _moduleNameString = value; }
        }

        [Category("My Properties")]
        [DefaultValue(true)]
        public string HeaderForm
        {
            get
            {
                return _titleText;
            }

            set
            {
                _titleText = value;
                this.Text = _titleText;
                pnlTitle.Text = _titleText;
            }
        }


        #endregion

        #region Override Methods

        protected override void LoadData()
        {
            base.LoadData();

        }

        protected override int UpdateData()
        {
            try
            {
                _bindingSource.EndEdit();
                _hasChanged = false;
                tsbCancel.Enabled = false;
                pnlChiTiet.Enabled = false;
                tsbLuu.Enabled = false;

                //return Cache.Context.SaveChanges(); //
                return base.UpdateData();
            }
            catch(Exception ex)
            {
                _hasChanged = true;
                tsbCancel.Enabled = true;
                pnlChiTiet.Enabled = true;
                tsbLuu.Enabled = true;

                UICommon.ShowMessgeString(ex.Message, "Lỗi dữ liệu");
                return -1;
            }
        }

        #endregion

        #region Virtual Methods

        protected virtual void ImportFormExcel()
        {

        }

        protected virtual void ExportToExcel()
        {
        }

        protected virtual void AddNew()
        {
            _bindingSource.EndEdit();
            _bindingSource.AddNew();
            _bindingSource.ResetBindings(true);

            _hasChanged = true;
            //tsbThem.Enabled = false;
            tsbCancel.Enabled = true;
            pnlChiTiet.Enabled = true;
            tsbLuu.Enabled = true;
        }

        protected virtual void Edit()
        {
            pnlChiTiet.Enabled = true;
            tsbLuu.Enabled = true;
        }

        protected virtual void Delete()
        {
            DialogResult result = MessageBox.Show("Bạn chắc là muốn xóa dòng dữ liệu này ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {

                _bindingSource.EndEdit();
                _bindingSource.RemoveCurrent();
                _hasChanged = true;
                tsbLuu.Enabled = true;

                Cache.Context.SaveChanges();
            }
        }

        protected virtual void RefreshData()
        {
            LoadData();
            _bindingSource.MoveFirst();
        }

        protected virtual void Report()
        {

        }

        protected virtual void Clear()
        {

        }

        protected virtual void Filter()
        {

        }

        protected virtual void Search()
        {

        }

        protected virtual void CancelAddinng()
        {

            //_bindingSource.CancelEdit();
            //_bindingSource.r

            RollBack();
            _hasChanged = false;
            //tsbThem.Enabled = true;
            //tsbCancel.Enabled = false;
            pnlChiTiet.Enabled = false;
            tsbLuu.Enabled = false;
        }

        protected virtual void InitForm()
        {

        }

        protected virtual void AddDataBinding()
        {

        }

        #endregion

        #region Protected Methods

        protected void ImportDataFromExcel<T>(DataGridView dgv)
        {
            try
            {
                frmImportExcel frm = new frmImportExcel();
                frm.ShowDialog();

                if (frm._data)
                {
                    List<T> listCustomer = frm.GetData<T>();

                    foreach (T item in listCustomer)
                    {
                        _bindingSource.Add(item);
                    }
                    _bindingSource.EndEdit();
                    dgv.DataSource = _bindingSource;
                    tsbLuu.Enabled = true;
                    _hasChanged = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi");
            }

        }

        protected void ExportDataToExcel(string sheetName, DataGridView dgv)
        {
            try
            {
                frmExportExcel frm = new frmExportExcel(sheetName, dgv);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi");
            }

        }

        protected void ExportDataToExcel(DataGridView dgv)
        {
            try
            {
                frmExportExcel frm = new frmExportExcel(dgv);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi");
            }

        }

        #endregion

        #region public Methods

        public void PermissionForUser(int pIDNhomNguoiDung)
        {
            //phan quyen
            try
            {
                //QuanLyBanHangEntities ctx = new QuanLyBanHangEntities();
                QuanLyBanHangEntities ctx = new QuanLyBanHangEntities();
                var pq = ctx.ACL_PhanQuyenNhomNguoiDung.Where(s => s.MaModule == ModuleNameString && s.IDNhomNguoiDung == pIDNhomNguoiDung).FirstOrDefault();
                if (pq != null)
                {
                    this.tsbThem.Enabled = this.tsbCancel.Visible = pq.Them ?? false;
                    this.tsbXoa.Enabled = pq.Xoa ?? false;
                    this.tsbSua.Enabled = this.pnlChiTiet.Enabled = pq.Sua ?? false;
                }
            }
            catch (Exception ex)
            {
                UICommon.ShowMessegeInfo(Const.MSG_LOI_PHAN_QUYEN);
            }
        }

        public void PermissionForUser()
        {
            //phan quyen
            try
            {
                if (UICommon.CurrentUserInfo != null)
                {
                    _groupUserID = UICommon.CurrentUserInfo.IDNhomNguoiDung == null ? null : UICommon.CurrentUserInfo.IDNhomNguoiDung;
                }

                if (_groupUserID == null)
                {
                    return;
                }

                QuanLyBanHangEntities ctx = new QuanLyBanHangEntities();
                var pq = ctx.ACL_PhanQuyenNhomNguoiDung.Where(s => s.MaModule == ModuleNameString && s.IDNhomNguoiDung == _groupUserID).FirstOrDefault();
                if (pq != null)
                {
                    this.tsbThem.Enabled = this.tsbCancel.Visible = pq.Them ?? false;
                    this.tsbXoa.Enabled = pq.Xoa ?? false;
                    this.tsbSua.Enabled = this.pnlChiTiet.Enabled = pq.Sua ?? false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi phân quyền");
            }
        }

        #endregion

        #region Events

        private void tsbClose_Click(object sender, EventArgs e)
        {
            //Config before Close this form
            this.Close();
        }

        private void tsbThem_Click(object sender, EventArgs e)
        {
            try
            {
                AddNew();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbSua_Click(object sender, EventArgs e)
        {
            try
            {
                Edit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbXoa_Click(object sender, EventArgs e)
        {
            try
            {
                Delete();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbLuu_Click(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void tsbBaoCao_Click(object sender, EventArgs e)
        {
            try
            {
                Report();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbLamMoi_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbClear_Click(object sender, EventArgs e)
        {
            try
            {
                Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbLoc_Click(object sender, EventArgs e)
        {
            try
            {
                Filter();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmManager_Load(object sender, EventArgs e)
        {
            try
            {
                LoadData();
                AddDataBinding();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                Search();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbImportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                ImportFormExcel();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbCancel_Click(object sender, EventArgs e)
        {
            try
            {
                CancelAddinng();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //xuat excel
        private void tsbExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                ExportToExcel();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion
    }
}
